import sortedcontainers
import collections
class TweetCounts(object):

    def __init__(self):
        self.d = collections.defaultdict(lambda: sortedcontainers.SortedList())

    def recordTweet(self, tweetName, time):
        """
        :type tweetName: str
        :type time: int
        :rtype: None
        """
        self.d[tweetName].add(time)



    def getTweetCountsPerFrequency(self, freq, tweetName, startTime, endTime):
        """
        :type freq: str
        :type tweetName: str
        :type startTime: int
        :type endTime: int
        :rtype: List[int]
        """
        if freq == 'minute':
            dist = 60
        if freq == 'hour':
            dist = 3600
        if freq == 'day':
            dist = 86400
        out = []
        time = startTime
        while time <= endTime:
            f_time = time
            end_time = min(time + dist - 1, endTime)
            start_index = self.d[tweetName].bisect_left(f_time)
            end_index = self.d[tweetName].bisect_right(end_time)
            out.append(end_index - start_index)
            time = end_time + 1
        return out
